package com.haixiaoke.flat.contract.controller;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.haixiaoke.auth.AuthCheck;
import com.haixiaoke.saas.common.constant.ContractConstants;
import com.haixiaoke.saas.common.core.domain.AjaxResult;
import com.haixiaoke.saas.common.core.page.TableDataInfo;
import com.haixiaoke.saas.common.exception.ServiceException;
import com.haixiaoke.saas.contract.domain.LesseeContract;
import com.haixiaoke.saas.contract.service.ILesseeContractService;
import com.haixiaoke.saas.system.BaseController;
import com.haixiaoke.token.FrontTokenComponent;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 租客合同Controller
 *
 * @author panda
 * @date 2024-06-04
 */
@RestController
@RequestMapping("/front/flat/contract/lesseeContract")
public class FlatLesseeContractController extends BaseController {
    @Autowired
    private ILesseeContractService lesseeContractService;
    @Autowired
    private FrontTokenComponent frontTokenComponent;

    /**
     * 我的合同列表查询租客合同列表
     */
    @AuthCheck
    @GetMapping("/lessees/list")
    public TableDataInfo listByConUser(LesseeContract lesseeContract) {
        startPage();
        lesseeContract.setConUserId(frontTokenComponent.getUserIdNotNull());
        List<LesseeContract> list = lesseeContractService.flatSelectLesseeContractList(lesseeContract);
        return getDataTable(list);
    }

    /**
     * 查询租客合同列表
     */
    @AuthCheck
    @GetMapping("/proprietor/list")
    public TableDataInfo list(LesseeContract lesseeContract) {
        startPage();
        lesseeContract.setProprietorUserId(frontTokenComponent.getUserIdNotNull());
        lesseeContract.setSigningMode(ContractConstants.SIGNING_MODE_PROPRIETOR);
        List<LesseeContract> list = lesseeContractService.flatSelectLesseeContractList(lesseeContract);
        return getDataTable(list);
    }

    /**
     * 获取租客合同详细信息
     */
    @AuthCheck
    @GetMapping(value = "/{lesseeContractId}")
    public AjaxResult getInfo(@PathVariable("lesseeContractId") String lesseeContractId) {
        return success(lesseeContractService.selectLesseeContractById(lesseeContractId));
    }


    /**
     * 获取预览电子合同内容链接
     */
    @PostMapping(value = "/previewUrl")
    public AjaxResult electronicVisaPreviewUrl(@RequestBody LesseeContract lesseeContract) {
        return AjaxResult.success("操作成功", lesseeContractService.selectLesseeContractElectronicVisaPreviewUrl(lesseeContract));
    }

    /**
     * 确认合同
     */
    @AuthCheck
    @PutMapping("/confirm/{lesseeContractId}")
    public AjaxResult confirm(@PathVariable("lesseeContractId") String lesseeContractId) {
        LesseeContract contract = lesseeContractService.selectLesseeContractById(lesseeContractId);
        if (ObjectUtil.isEmpty(contract)) {
            return error("合同不存在");
        }
        if (!contract.getProprietorUserId().equals(frontTokenComponent.getUserIdNotNull())) {
            return error("非法操作");
        }
        if (!ContractConstants.SIGNING_MODE_PROPRIETOR.equals(contract.getSigningMode())) {
            return error("合同签署方式异常,无需确认");
        }
        if (!ContractConstants.LESSEE_CONTRACT_STATUS_UNCONFIRMED.equals(contract.getLesseeContractStatus())) {
            return error("合同状态异常，请刷新后再试");
        }
        contract.setLesseeContractStatus(ContractConstants.LESSEE_CONTRACT_STATUS_BE_SIGNING);
        return toAjax(lesseeContractService.updateLesseeContract(contract));
    }

    /**
     * 合同撤销
     */
    @AuthCheck
    @PutMapping("/repeal")
    public AjaxResult repeal(@RequestBody LesseeContract lesseeContract) {
        LesseeContract contract = lesseeContractService.selectLesseeContractById(lesseeContract.getLesseeContractId());
        if (ObjectUtil.isEmpty(contract)) {
            return error("合同不存在");
        }
        if (!contract.getProprietorUserId().equals(frontTokenComponent.getUserIdNotNull())) {
            return error("非法操作");
        }
        if (!(ContractConstants.LESSEE_CONTRACT_STATUS_UNCONFIRMED.equals(contract.getLesseeContractStatus()) ||
                ContractConstants.LESSEE_CONTRACT_STATUS_BE_SIGNING.equals(contract.getLesseeContractStatus()))) {
            return error("合同状态异常，请刷新后再试");
        }
        contract.setUpdateBy(frontTokenComponent.getLoginUser().getUser().getUserName());
        return toAjax(lesseeContractService.repealLesseeContract(contract));
    }

    /**
     * 租客签订合同
     */
    @AuthCheck
    @PostMapping("/lessees/signing")
    public AjaxResult lesseesSigning(@RequestBody LesseeContract lesseeContract) {
        return lesseeContractService.signingLesseeContract(lesseeContract, true, frontTokenComponent.getUserIdNotNull());
    }

    /**
     * 业主签订合同
     */
    @AuthCheck
    @PostMapping("/proprietor/signing")
    public AjaxResult proprietorSigning(@RequestBody LesseeContract lesseeContract) {
        return lesseeContractService.signingLesseeContract(lesseeContract, false, frontTokenComponent.getUserIdNotNull());
    }

    /**
     * 删除租客合同
     */
    @AuthCheck
    @DeleteMapping("/{lesseeContractId}")
    public AjaxResult remove(@PathVariable("lesseeContractId") String lesseeContractId) {
        LesseeContract contract = lesseeContractService.selectLesseeContractById(lesseeContractId);
        if (!(frontTokenComponent.getUserIdNotNull().equals(contract.getProprietorUserId()) ||
                frontTokenComponent.getUserIdNotNull().equals(contract.getConUserId()))) {
            return error("非法操作");
        }
        if (ContractConstants.LESSEE_CONTRACT_STATUS_UNCONFIRMED.equals(contract.getLesseeContractStatus()) ||
                ContractConstants.LESSEE_CONTRACT_STATUS_BE_SIGNING.equals(contract.getLesseeContractStatus())) {
            return error("合同状态异常，请刷新后再试");
        }
        return toAjax(lesseeContractService.frontDeletedLesseeContract(lesseeContractId));
    }
}
